import { GameData } from "../game/GameData";
import { GameState } from "../game/GameState";
import { TaskMgr } from "../game/TaskMgr";
var SignReward = [100, 150, 200, 250, 300, 350, 500];
cc.Class({
    extends: require('BasePanel'),

    properties: {
        nodBtnDay: [cc.Node],
        toggle: cc.Toggle,
    },
    onLoad() {
        this._super();
    },

    onEnter(args, action = true) {
        this._super(args, action);
        this._init();
        this.toggle.isChecked = true;
    },

    onExit(args) {
        this._super(args);
        G.ad.hide(G.ad.AD_BANNER);
    },

    _init() {
        this._signDay = GameData.getSignDay();
        this._hadSign = GameData.getHadSigned();

        // 设置当天  如果签到过了，则天数-1
        let index;
        if (this._hadSign) {
            index = (this._signDay - 1) % 7;
        } else {
            index = this._signDay % 7;
        }
        this.nodBtnDay[index].getChildByName('qipao').active = false;
        this.nodBtnDay[index].getChildByName('qipao2').active = true;
        this.nodBtnDay[index].getChildByName('duigou').active = this._hadSign;

        for (let i = 0; i < index; i++) {
            this.nodBtnDay[i].getChildByName('duigou').active = true;
        }
    },

    show() {
        this._super();

    },

    hide(cb) {
        this._super(cb);
    },
    onEnable() {

    },
    onDisable() {
    },

    buttonListener: function (button) {
        G.game.gameAudioMgr.playBtn();
        let name = button.node.name;
        if (name == 'btnClose') {
            this.onClose();
        } else if (name == 'btnSign') {
            if (this._hadSign) {
                G.game.toast('你已经签到过了哦！', 1);
                return;
            }
            this._hadSign = true;
            let toggleChecked = this.toggle.isChecked;

            GameData.addSignDay();

            if (toggleChecked) {


                G.ad.show(G.ad.AD_VIDEO, (param) => {
                    G.partner.sendEvent('签到', { '类型': '签到' });
                    if (param.event == G.ad.EVENT_SHOW_SUCC) {
                        G.partner.sendEvent('视频广告', { '类型': '签到' });
                        this.onClose();
                        G.game.toast('钻石+' + SignReward[this._signDay] * 2, 1);
                        TaskMgr.addDaliyTask(5, 1);
                    } else {
                        G.partner.share({ title: '快来领养一只可爱的兔兔吧' }, () => {
                            G.partner.sendEvent('分享', { '类型': '签到' });
                            this.onClose();
                            G.game.toast('钻石+' + SignReward[this._signDay] * 2, 1);
                            TaskMgr.addDaliyTask(5, 1);
                        });
                    }
                })
            } else {
                this.onClose();
                G.game.toast('钻石+' + SignReward[this._signDay], 1);
            }
        }
    },

    onTouchStart() {

    },
});
